<?php
require_once('../../config/config.inc');
header('Content-Type: text/html; charset=utf-8');
//Connect to mysql server
$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
if(!$link) {
	die('Failed to connect to server: ' . mysql_error());
}

//Select database
$db = mysql_select_db(DB_DATABASE);
if(!$db) {
	die("Unable to select database");
}
mysql_set_charset('utf8',$db);
mysql_query("SET CHARACTER SET utf8");
mysql_query("SET NAMES utf8_persian_ci");

if (file_exists("../upload_img/" . $_FILES["bookImg"]["name"]) || file_exists("../upload_file/" . $_FILES["bookFile"]["name"])){

	echo "Files already exists";

}else{

	$file = $_FILES["bookFile"]["name"];
	$img = $_FILES["bookImg"]["name"];
	$filePath = "../upload_file/" . $file;
	$imgPath = "../upload_img/" . $img;
	
	if(move_uploaded_file($_FILES["bookFile"]["tmp_name"], $filePath) && move_uploaded_file($_FILES["bookImg"]["tmp_name"], $imgPath)){
		$sql = "INSERT INTO `books` VALUES (null,'".$_POST['bookName']."','".$_POST['bookAuthor']."','".$_POST['bookDesc']."', '$file', '$img')";
		if (mysql_query($sql)) { // Error handling
			header("Location:../index.php");
		}
		else{
			echo mysql_error();
		}


			}else{

			echo "File not uploaded there are an error <strong>{$file}</strong>";

			}
			

			} ?>
